package com.zzyl.task;

import com.zzyl.entity.Reservation;
import com.zzyl.enums.ReservationStatus;
import com.zzyl.mapper.ReservationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class ReservationTask {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 定时任务：处理一直处于派送中的订单
     */

    @Scheduled(cron = " 0 1,31 */1 * * ?")// 每小时的一分钟和31分钟自动检查
    public void processReservation() {
        log.info("定时任务：处理未按预约时间到访的预约订单：{}", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now();
        List<Reservation> reservations = reservationMapper.getByOverReservation(ReservationStatus.PENDING.getOrdinal(), time);
        if (reservations != null && reservations.size() > 0) {
            for (Reservation reservation : reservations) {
                // 2. 超时订单改为取消订单
                reservation.setStatus(ReservationStatus.CANCELED.getOrdinal());
                reservationMapper.update(reservation);
            }
        }
    }
}
